package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.MultiClassroom;
import com.shycloud.mido.member.api.entity.OrgTeacherDetailed;
import com.shycloud.mido.member.api.vo.AppMultiStudentClassroomVO;
import com.shycloud.mido.member.service.MemberServiceV2;
import com.shycloud.mido.member.service.MultiClassroomService;
import com.shycloud.mido.member.service.MultiTeacherClassroomLinkService;
import com.shycloud.mido.member.service.OrgTeacherDetailedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2021-07-12
 */
@Api(tags = "教室操作")
@RestController
@RequestMapping("/v1/multi-classroom")
@AllArgsConstructor
public class MultiClassroomController {

	/** 教室模块 service */
	private MultiClassroomService classroomService;
	/** 教师模块 service */
	private MultiTeacherClassroomLinkService teacherClassroomService;
	/** 用户模块 service */
	private MemberServiceV2 memberService;

	@ApiOperation(value = "后台管理-APP-添加教室接口", notes = "传 name,orgId,scope(作用域 1-APP,2-sys(此时要显示[机构]二字)) ")
	@PostMapping("addMultiClassRoom")
	public R addMultiClassRoom(@RequestBody MultiClassroom classroom) {

		classroom.setCreateTime(LocalDateTime.now());
		//APP的场合,创建人为登录者
		if ("1".equals(classroom.getScope())) {
			classroom.setCreateUser(SecurityUtils.getMember().getId());
		} else {
			classroom.setScope("2");
			//后台管理的场合为机构管理员
			classroom.setCreateUser("sys");
		}
		classroomService.save(classroom);
		if ("1".equals(classroom.getScope())) {
			Member member = memberService.getById(SecurityUtils.getMember().getId());
			teacherClassroomService.addTeacherApp(member.getUsername(), classroom);
		}
		return new R(true);
	}

	@ApiOperation(value = "后台管理-删除教室接口", notes = "只传id")
	@PostMapping("delMultiClassRoom")
	public R delMultiClassRoom(@RequestBody MultiClassroom classroom) {
		return new R(classroomService.delMultiClassRoom(classroom));
	}

	@ApiOperation(value = "后台管理-编辑教室接口", notes = "只传id,name")
	@PostMapping("updMultiClassRoom")
	public R updMultiClassRoom(@RequestBody MultiClassroom classroom) {
		return new R(classroomService.updateById(classroom));
	}

	@ApiOperation("后台管理-获取教室列表")
	@GetMapping("getMultiClassRoomList")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "size", value = "每页显示条数", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "orgId", value = "机构ID", paramType = "query", dataType = "int", required = true)
	})
	public R getMultiClassRoomPage(Integer current, Integer size, Integer orgId, String name) {
		return new R(classroomService.getMultiClassRoomPage(new Page(current, size), orgId, name));
	}

	@ApiOperation("APP-获取班级下学生/教师列表")
	@GetMapping("getClassRoomDetail")
	public R getClassRoomDetail(String teacherId, String studentId) {
		return new R(classroomService.getClassRoomDetail(teacherId, studentId));
	}

	@ApiIgnore
	@ApiOperation("Feign-根据学生教师获取所在班级ID,无则返回 null")
	@GetMapping("getClassroomId")
	public String getClassroomId(String teacherId, String studentId) {
		return classroomService.getClassRoomId(teacherId, studentId);
	}

}
